package com.gcorp.idealgas;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.gcorp.idealgas.type.CustomMolecule;
import com.gcorp.idealgas.type.StandardMoleculeWithRandomSpeed;

class AddNewMoleculeDialog extends JDialog
{
	private static AddNewMoleculeDialog instance;
	
	private JPanel panel;
	
	private JLabel     setTypeText;
	
	private JComboBox  setType;
	
	private JLabel     setMass;
	
	private JTextField getMass;
	
	private JLabel     setRadius;
	
	private JTextField getRadius;
	
	private JButton    getColor;
	
	private JLabel     setColor;
	
	private JLabel     setCoords;
	
	private JButton    getCoords;
	
	private JLabel     setInitialVelocity;
	
	private JLabel     setVelocity;
	
	private JTextField getVelocityX;
	
	private JTextField getVelocityY;
	
	
	
	private Color newColor = Color.red;
	
	private boolean waitForCoords;
	
	private float x;
	
	private float y;
	
	private AddNewMoleculeDialog()
	{
		initUI();
		
		x = y = 0;
		
	}
	public static AddNewMoleculeDialog getInstance()
	{
		if(instance == null)
		{
			instance = new AddNewMoleculeDialog();
		}
		
		return instance;
	}
	public boolean isWaitForCoordsState()
	{
	    return waitForCoords;
	}
	public void setWaitingForCoordsState(boolean waitForCoords)
	{
	    this.waitForCoords = waitForCoords;
	}
	public void setX(float x)
	{
	    this.x = x;
	}
	public void setY(float y)
	{
	    this.y = y;
	}
	public void hideWindow()
	{
	    setVisible(false);
	}
	public void showWindow()
	{
	    setVisible(true);
	}
	private void addElement(Container c, Component e,int x,int y,int h,int w)
	{
	    e.setBounds(x, y, h, w);
	    c.add(e);
	}
	private void makePanel()
	{
		panel = (JPanel)getContentPane();
		panel.setLayout(null);
	}
	private void makeMoleculeTypeChooser()
	{
		String [] items = {
	            "Random Type",
	            "Custom Type"
	    };
		setTypeText  = new JLabel("Set the type ");
		setType = new JComboBox(items);
		setType.setSelectedIndex(1);
		
		addElement(panel,setTypeText,20,20,100,20);
        addElement(panel,setType,120,20,100,20);
	}
	
	private void makeMoleculeTypeChooserListener()
	{
		setType.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) 
			{
				if(setType.getSelectedIndex() == 0)
				{
					getMass.setEnabled(false);
					getRadius.setEnabled(false);
					getColor.setEnabled(false);
					getVelocityX.setEnabled(false);
					getVelocityY.setEnabled(false);
				}
				else
				{
					getMass.setEnabled(true);
					getRadius.setEnabled(true);
					getColor.setEnabled(true);
					getVelocityX.setEnabled(true);
                    getVelocityY.setEnabled(true);
				}
			}
			
		});
	}
	private void makeMassGetter()
	{
		setMass = new JLabel("Mass ");
		getMass = new JTextField("6.02");
		
		addElement(panel,setMass,20,60,100,20);
		addElement(panel,getMass,120,60,100,20);
	}
	private void makeRadiusGetter()
	{
		setRadius = new JLabel("Radius ");
        getRadius = new JTextField("7.0");
        
        addElement(panel,setRadius,20,80,100,20);
        addElement(panel,getRadius,120,80,100,20);
	}
	private void makeColorGetter()
	{
		getColor  = new JButton("Press the key");
		setColor  = new JLabel("Set the color");
		
		addElement(panel,setColor,20,100,100,20);
        addElement(panel,getColor,120,100,100,20);
	}
	private void makeCoordsGetter()
	{
	    setCoords = new JLabel("Set the coords");
	    getCoords = new JButton("Press the key");
	    getCoords.addActionListener(new ActionListener()
	    {

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                waitForCoords = true;
                hideWindow();
            }
	        
	    });
	    
	  
	    addElement(panel,setCoords,20,120,100,20);
	    addElement(panel,getCoords,120,120,100,20);
	}
	private void makeColorGetterListener()
	{
		getColor.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                Color col = JColorChooser.showDialog(AddNewMoleculeDialog.this,
                        "Choose Background Color",newColor);
                
                newColor = col;
            }
            
        });
	}
	private void makeVelocityGetter()
	{
	    setVelocity  = new JLabel("Set the velocity");
	    getVelocityX = new JTextField("-1.0");
	    getVelocityY = new JTextField("1.0");
	    
	    
	    addElement(panel,setVelocity,20,140,100,20);
        addElement(panel,getVelocityX,120,140,50,20);
        addElement(panel,getVelocityY,170,140,50,20);
	}
	public void makeAddButton()
	{
	    JButton addButton = new JButton("Add");
        

        addButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                Molecule m = new Molecule(x,y , setType.getSelectedIndex() == 0?new StandardMoleculeWithRandomSpeed() : 
                    new CustomMolecule(Float.parseFloat(getMass.getText()),Float.parseFloat(getRadius.getText()),newColor,
                                       Float.parseFloat(getVelocityX.getText()),Float.parseFloat(getVelocityY.getText())), 1);
                
                Simulation.getInstance().addNewMolecule(m);
                Simulation.getInstance().redraw();
                setVisible(false);
                
            }
            
        });
        addElement(panel,addButton,20,220,100,20);
	}
	public void initUI()
	{
		
		makePanel();
	    
		makeMoleculeTypeChooser();
		makeMoleculeTypeChooserListener();
		makeMassGetter();
		makeRadiusGetter();
		makeColorGetter();
		makeColorGetterListener();
		makeCoordsGetter();
		makeVelocityGetter();
		makeAddButton();
		

		
		setTitle("Add New Molecule");
		setSize(400,300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		
	}
	public void reset() 
	{
		//initUI();
	    
	}
}